package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.auth.login.annotation.NeedLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;


    // Request URL: http://localhost:8500/api/payment/wxPay/createJsapi/1301/x2zWLoLpKCIm
    @PostMapping("createJsapi/{paymentType}/{orderNo}")
    @Operation(summary = "创建微信支付二维码")
    @NeedLogin
    public Result<Map<String, Object>> createJsapi(@PathVariable String paymentType, @PathVariable String orderNo) {
        Map<String, Object> map = wxPayService.createJsapi(paymentType, orderNo);
        return Result.ok(map);
    }

    @Operation(summary = "支付状态查询")
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result<Boolean> queryPayStatus(@PathVariable String orderNo) {
        return wxPayService.queryPayStatus(orderNo);
    }

    @Operation(summary = "微信支付异步通知接口")
    @PostMapping("/asyncNotify")
    public Map<String, Object> asyncNotify(HttpServletRequest request) {
        try {
            wxPayService.asyncNotify(request);
            //返回成功
            Map<String, Object> result = new HashMap<>();
            result.put("code", "SUCCESS");
            result.put("message", "成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败
            Map<String, Object> result = new HashMap<>();
            result.put("code", "FAIL");
            result.put("message", "失败");
            return result;
        }
    }

    // Request URL: http://localhost:8500/api/payment/wxPay/createNative/1301/1
    @PostMapping("createNative/{paymentType}/{orderNo}")
    @Operation(summary = "测试生成微信支付二维码")
    @NeedLogin
    @Parameters({
            @Parameter(name = "paymentType",description = "支付类型：1301-订单 1302-充值",in = ParameterIn.PATH,required = true),
            @Parameter(name = "orderNo",description = "订单号",required = true,in = ParameterIn.PATH),
    })
    public Result<Map<String, Object>> createNative(@PathVariable String paymentType, @PathVariable String orderNo) {
        Map<String, Object> map = wxPayService.createNative(paymentType, orderNo, AuthContextHolder.getUserId());
        return Result.ok(map);
    }

}
